<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.hahehiho.myalbum.MyAlbumVO"%>
<%@page import="com.hahehiho.myalbum.MyAlbumDB"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0,width=device-width" />
<title>MyAlbum</title>
<link rel="stylesheet" href="css/default.css" >

</head>
<%
String userAgent = request.getHeader("user-agent");
int tdsize = 1;
if(userAgent.matches(".*BlackBerry.*")) {
} else if(userAgent.matches(".*Android.*")) {
} else if(userAgent.matches(".*iPhone.*")) {
} else if(userAgent.matches(".*iPad.*")) {
	tdsize = 2;
} else {
	tdsize = 4;
}

String[] addrs = request.getParameterValues("addrs");

String isGroup = request.getParameter("isgroup");
if(isGroup == null) {
	isGroup = "true";
}

if(addrs == null)
	addrs = new String[0];

MyAlbumDB db = MyAlbumDB.getInstance();
MyAlbumVO[] vos = db.selectPhotoListByGPS(addrs, isGroup);

String groupLinkString = "";
String groupLinkDesc = "show all files";
String willIsGroup = "false";

if(isGroup.equalsIgnoreCase("false") ) {
	groupLinkDesc = "show group";
	willIsGroup = "true";
}


String header = "<a href=\"index.html\">HOME</a> | <a href=\"viewgps.jsp?isgroup="+isGroup+"\">Country</a> | ";
String linkaddrs = "";
for(int i = 0; i < addrs.length; i++) {
	linkaddrs += "&addrs=" + addrs[i];
	String link = "viewgps.jsp?depth=month&isgroup="+isGroup+linkaddrs;
	header += "<a href=\""+link+"\">" + addrs[i] + "</a>&nbsp;";	
}

groupLinkString = "viewgps.jsp?isgroup="+willIsGroup;	
groupLinkString += linkaddrs;
%>
<body>
<table style="width:100%"><tr><td align="left" width="90%">
<h3><%=header%></h3>
</td>
<td width="10%" align="right"><a href="<%=groupLinkString%>"><%=groupLinkDesc%></a></td>
</tr></table>
<hr>
<table style="width:95%">
<%
int trindex = 0;

if(vos.length == 1)
	isGroup = "false";

for(int i = 0; i < vos.length; i++) {
	if((i%tdsize) == 0) {
		trindex = i;
%>
<tr>
<%
	}
%>
<td align=center>
<%
	String linkString = "";
	String descString = "";
	
	String countString = "";
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
	
	if(vos[i].getCount() > 1) {
		countString = "(" + vos[i].getCount() + ")";
		linkString = "viewgps.jsp?isgroup=" + isGroup + linkaddrs + "&addrs=" + vos[i].getAddrs()[vos[i].getAddrs().length-1];
	} else {
		linkString = "viewpic.jsp?pkey="+vos[i].getpKey();
	}
	
	descString = vos[i].getFullAddr() + "<br>" + sdf.format(vos[i].getOrgDate().getTime());		
%>
<a href="<%=linkString%>"><img src=<%=vos[i].getThumbNailFile() %>><br><%=descString%> <%=countString%></a>
</td>
<%	
	if((i == (trindex+tdsize)) || (i == (vos.length-1))) {
%>
</tr>
<%
	}
}
%>
</table>
</body>
</html>